package com.jch.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jch.eduservice.entity.EduChapter;
import com.jch.eduservice.entity.EduVideo;
import com.jch.eduservice.entity.chapter.ChapterVo;
import com.jch.eduservice.entity.chapter.videoVo;
import com.jch.eduservice.mapper.EduChapterMapper;
import com.jch.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jch.eduservice.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author jch
 * @since 2024-12-21
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
     @Autowired
     private EduVideoService videoService;
    @Override
    public List<ChapterVo> getChapterVideo(String courceId) {
        //根据课程id查询所有章节
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courceId);
        List<EduChapter> chapterList = baseMapper.selectList(wrapper);
        //根据章节id查询小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courceId);
        List<EduVideo> videoList = videoService.list(wrapperVideo);
        List<ChapterVo> chapterVoList = new ArrayList<>();
        //遍历所有章节
        for (EduChapter eduChapter : chapterList) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //遍历所有小节
            List<videoVo> eduVideoList = new ArrayList<>();
            for (EduVideo eduVideo : videoList) {
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    videoVo eduVideoVo = new videoVo();
                    BeanUtils.copyProperties(eduVideo,eduVideoVo);
                        eduVideoList.add(eduVideoVo);

                }
            }
            chapterVo.setChildren(eduVideoList);
            chapterVoList.add(chapterVo);
        }
        return chapterVoList;
    }
}
